/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Log4jLogPrinter.java 378 2007-02-08 23:30:38Z jacek.kolezynski $
 */
package logmatcher.analyzers;

import logmatcher.InitializationException;
import logmatcher.Utils;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.apache.log4j.Layout;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.spi.LoggingEvent;


/**
 *  Simplest implementation of MessageListener, logs out received messages using log4j logging.
 *
 *  @version $Revision: 378 $
 */
public class Log4jLogPrinter extends AnalyzerSkeleton {
    //~ Static fields/initializers -------------------------------------------------------------------------------------

    /** Class logger. */
    private static final Log LOGGER = LogFactory.getLog(Log4jLogPrinter.class);

    //~ Instance fields ------------------------------------------------------------------------------------------------

    /** Output strings formatter. */
    private Layout layout;

    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
     * Creates LogPrinter with default config.
     *
     */
    public Log4jLogPrinter() {
        super();
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     *  Read the object properties from configuration source.
     *
     *  @see logmatcher.Configurable#configure(org.apache.commons.configuration.Configuration, java.lang.String)
     */
    public void configureAnalyzer(Configuration config, String id)
        throws InitializationException {
        if (config.containsKey(id + ".layout.class")) {
            Object ob = Utils.loadObject(id + ".layout.class");

            if (ob instanceof Layout) {
                layout = (Layout)ob;

                if (ob instanceof PatternLayout) {
                    String s = config.getString(id + ".layout.pattern");
                    ((PatternLayout)ob).setConversionPattern(s);
                }
            }
        } else {
            layout = new SimpleLayout();
        }
    }


    /**
     *  Handle incomming message.
     *
     *  @param message object to be logged.
     */
    public void receiveLogEvent(Object message) {
        if (message instanceof LoggingEvent) {
            LOGGER.info(layout.format((LoggingEvent)message));
        } else {
            LOGGER.info(message);
        }
    }


    /**
     *  Get formatting object.
     *
     *  @return the layout.
     */
    public Layout getLayout() {
        return layout;
    }


    /**
     *  Set output formatting controller.
     *
     *  @param layout the layout to set.
     */
    public void setLayout(Layout layout) {
        this.layout = layout;
    }
}
